﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Project.MVC.DataAccess;
using Project.MVC.Admin.Util;
using Project.MVC.Admin.Models;
using System.Data.Linq;


namespace Project.MVC.Admin.Controllers
{
    public class ProductController : Controller
    {
        ProductAccess _productAccess = new ProductAccess();
        CategoryAccess _categoryAccess = new CategoryAccess();
        PictureAccess _pictureAccess = new PictureAccess();
        public ActionResult Index(int id = 1)
        {
            var model = new ProductModel();
            //model.Type = type;
            model.PageList = _productAccess.GetList().ToPagedList(id, 5);
            return View("Index", model);
        }

        public ActionResult Add()
        {
            var model = new ProductModel();
            //model.Type = type;
            model.Product = new Product();
            model.ParentCategories = GetListCategory();
            return View("Add", model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(FormCollection form, ProductModel model)
        {
            model = GetModel(form, true);
            model.ParentCategories = GetListCategory();
            _productAccess.Insert(model.Product);
            return View("Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var model = new ProductModel();
            model.Product = _productAccess.GetByKey(id);
            model.ParentCategories = GetListCategory();

            return View("Edit", model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FormCollection form, ProductModel model)
        {
            model = GetModel(form, false);
            model.ParentCategories = GetListCategory();
            _productAccess.Edit(model.Product);
            return View("Edit", model);
        }

        public ActionResult Delete(int id)
        {
            var model = new Product();
            model.Id = id;
            _productAccess.Delete(model);
            return RedirectToAction("Index");
        }

        public ActionResult ProductPictureAdd(string pictureURL, int displayOrder, int productId)
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
            //    return AccessDeniedView();

            if (string.IsNullOrEmpty(pictureURL))
                throw new ArgumentException();

            var product = _productAccess.GetByKey(productId);
            if (product == null)
                throw new ArgumentException("No product found with the specified id");
            var picture = new Picture()
            {
                PictureURL = pictureURL,
                ProductId = productId,
                DisplayOrder = displayOrder,                
            };
            _pictureAccess.Insert(picture);


            //_pictureService.SetSeoFilename(pictureId, _pictureService.GetPictureSeName(product.Name));

            //return Json(new { Result = true }, JsonRequestBehavior.AllowGet);
            //return HttpContext.Request.IsAjaxRequest() ? Json(picture, JsonRequestBehavior.AllowGet) : null;
            return Json(new { Id = picture.Id,PictureURL=picture.PictureURL,DisplayOrder=picture.DisplayOrder }, JsonRequestBehavior.AllowGet);
        }
        protected List<Category> GetListCategory()
        {
            return _categoryAccess.GetAllCategories(false, (int)EnumArticleType.Products).ToList();
        }

        protected string ShowMessage(string message)
        {
            string s = string.Empty;
            s += "<dl id=\"system-message\">";
            s += "<dd class=\"message message\"><ul>";
            s += "<li>" + message + "</li>";
            s += "</ul></dd>";
            s += "</dl>";
            return s;
        }
        protected ProductModel GetModel(FormCollection form, bool isNew)
        {
            var model = new ProductModel();
            if (!isNew)
                model.Product.Id = int.Parse(form.Get("hdId"));
            else
                model.Product.CreatedOnUtc = DateTime.Now;
            model.Product.Name = form.Get("txtName");
            model.Product.ShortDescription = form.Get("txtShortDescription");
            model.Product.FullDescription = form.Get("txtFullDescription");
            model.Product.Deleted = false;
            model.Product.UpdatedOnUtc = DateTime.Now;
            if ((form.Get("ddlCategoryId") != null) && (!string.IsNullOrEmpty(form.Get("ddlCategoryId"))))
                model.Product.CategoryId = int.Parse(form.Get("ddlCategoryId"));

            //if (form.Get("cbHot").Equals("false"))
            //    model.New.IsHot = false;
            //else
            //    model.New.IsHot = true;
            //if (form.Get("cbHome").Equals("false"))
            //    model.New.IsHome = false;
            //else
            //    model.New.IsHome = true;
            //model.New.IsHot = bool.Parse(form.Get("cbHot"));
            //model.New.IsHome = bool.Parse(form.Get("cbHome"));
            return model;
        }

        [HttpPost]       
        public JsonResult DeleteChoices(int choices)
        {
            var _object = _pictureAccess.GetByKey(choices);
            _pictureAccess.Delete(_object);
            return Json(true, JsonRequestBehavior.AllowGet);
        }

    }
}
